package com.sh.栈;

import java.util.Stack;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class 有效的括号 {
    public static void main(String[] args) {
        有效的括号 obj = new 有效的括号();
        obj = null; // 让对象变成垃圾
        // 等待一会给 GC 执行时间（不一定需要）
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("this.finalize();执行了");
    }

    public static boolean isValid(String s) {
        char[] charArray = s.toCharArray();
        Stack<Character> objects = new Stack<>();
        for (char c : charArray) {
            if (c=='('){
                objects.push(')');
            }else if (c=='{'){
                objects.push('}');
            }else if (c=='['){
                objects.push(']');
            }else {
                if (objects.pop()!=c || objects.isEmpty()){
                    return false;
                }
            }
        }
        return objects.isEmpty();
    }
}

